package 
{
	import flash.events.NetStatusEvent;
	import flash.external.ExternalInterface;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	
	import models.vo.UserVO;
	
	import org.dexter.DexterEvent;
	import org.dexter.DexterFramework;
	import org.dexter.sendDexterEvent;
	
	import vo.InitVO;
	
	public class FMS extends NetConnection
	{
		[DexterBinding]
		public var stratus:Stratus;
		public function FMS()
		{
			super();
			client = this;
			addEventListener(NetStatusEvent.NET_STATUS,onStatus);
		}
		private function onStatus(event:NetStatusEvent):void{
			sendDexterEvent("callJs","jsTrace","fms:"+event.info.code);
			trace(event.info.code);
			if(event.info.code == "NetConnection.Connect.Closed"){
				try{ExternalInterface.call("location.reload");}catch(e:Error){}
			}
		}
		public function initConnection(initObject:Object):void{
			var initVO:InitVO = new InitVO();
			initVO.convertFromObject(initObject);
			UserVO.self.id = initVO.id;
			UserVO.self.ip = initVO.ip;
			sendDexterEvent("initUserList",initVO.userList);
			sendDexterEvent("callJs","setSelfId",initVO.id);
			sendDexterEvent("initAppProperty",initVO.appProperty);
			if(stratus.hasConnected){
				stratusConnected();
			}else{
				DexterEvent.addDexterEvent("stratusConnected",this);
			}
		}
		public function stratusConnected():void{
			call("readyOnline",null,UserVO.self);
			sendDexterEvent("initPublishVideo");
		}
		public function userOnline(user:Object):void{
			sendDexterEvent("userOnline",user);
		}
		public function userOffline(userId:String):void{
			sendDexterEvent("userOffline",userId);
		}
		public function changeUserProperty(id:String,name:String,value:*):void{
			sendDexterEvent("changeUserProperty",id,name,value);
		}
		[DexterEvent][DexterExternal]
		public function changeUserPropertyFromJs(id:String,name:String,value:*):void{
			call("changeUserProperty",null,id,name,value);
		}
		[DexterEvent][DexterExternal]
		public function changeAppPropertyFromJs(name:String,value:*):void{
			call("changeAppProperty",null,name,value);
		}
		[DexterExternal]
		public function getDeviceConfig(userId:String):void{
			var user:UserVO = sendDexterEvent("getUserByID",userId);
			if(user&&user.isSelf)sendDexterEvent("callJs","setDeviceConfigDialog",sendDexterEvent("$getDeviceConfig"));
			else if(user) sendDexterEvent("sendToUser",user,"getDeviceConfig",UserVO.self.groupAddress);
		}
		public function changeAppProperty(name:String,value:*):void{
			sendDexterEvent("changeAppProperty",name,value);
		}
		[DexterExternal]
		public function windowClose():void{
			stratus.close();
			close();
		}
	}
}